package com.whfc.ms.api.emp.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.dto.AppFaceGateVisitorCheckDTO;
import com.whfc.emp.dto.AppFaceGateVisitorDTO;
import com.whfc.emp.dto.AppFaceGateVisitorNumDTO;
import com.whfc.emp.param.AppFaceGateVisitorAddParam;
import com.whfc.emp.param.AppFaceGateVisitorCheckAddParam;
import com.whfc.emp.param.AppFaceGateVisitorCheckParam;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.emp.service.MsAppFaceGateVisitorService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @ClasssName AppFaceGateVisitorController
 * @Description 访客审核
 * @Author hw
 * @Date 2021/2/26 16:42
 * @Version 1.0
 */
@RestController
@RequestMapping("/ms/api/emp/visitor/")
public class AppFaceGateVisitorController extends BaseController {

    @Autowired
    private MsAppFaceGateVisitorService msAppFaceGateVisitorService;

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam(value = "state", required = false) Integer state,
                       @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                       @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        logger.info("查询访客审批列表，loginer:{},deptId：{},pageSize：{},pageNum：{},state：{},startTime：{},endTime：{}",
                user.getUsername(), deptId, pageSize, pageNum, state, startTime, endTime);
        PageData<AppFaceGateVisitorDTO> pageData = msAppFaceGateVisitorService.list(deptId, pageSize, pageNum, state, startTime, endTime);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/getNum")
    public Result getNum(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("deptId") Integer deptId,
                         @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                         @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        logger.info("查询审核数量，loginer:{},deptId：{},startTime：{},endTime：{}",
                user.getUsername(), deptId, startTime, endTime);
        AppFaceGateVisitorNumDTO data = msAppFaceGateVisitorService.getNum(deptId, startTime, endTime);
        return ResultUtil.success(data);
    }

    @PostMapping(value = {"/anon/add", "/add"})
    public Result add(@Validated @RequestBody AppFaceGateVisitorAddParam request) {
        logger.info("访客申请，request：{}", request);
        msAppFaceGateVisitorService.add(request);
        return ResultUtil.success();

    }

    @RequiresPermissions("emp/audit/del")
    @PostMapping("/del/{visitorId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("visitorId") Integer visitorId) {
        logger.info("删除访客申请，visitorId：{}", visitorId);
        msAppFaceGateVisitorService.del(visitorId);
        return ResultUtil.success();

    }

    @RequiresPermissions("emp/audit/visitor")
    @PostMapping("/check")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @Validated @RequestBody AppFaceGateVisitorCheckParam request) {
        logger.info("访客审核，loginer:{},request：{}", user.getUsername(), request);
        request.setCheckName(user.getNickname());
        msAppFaceGateVisitorService.check(request);
        return ResultUtil.success();
    }

    @GetMapping("/export")
    public Result export(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("deptId") Integer deptId,
                         @RequestParam(value = "state", required = false) Integer state,
                         @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                         @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        logger.info("查询访客审批列表，loginer:{},deptId：{},state：{},startTime：{},endTime：{}",
                user.getUsername(), deptId, state, startTime, endTime);
        OssPathDTO data = msAppFaceGateVisitorService.export(deptId, state, startTime, endTime);
        return ResultUtil.success(data);
    }

    @PostMapping(value = {"/anon/checkAdd", "/checkAdd"})
    public Result checkAdd(@Validated @RequestBody AppFaceGateVisitorCheckAddParam request) {
        logger.info("访客校验信息是否存在，request：{}", request);
        AppFaceGateVisitorCheckDTO data = msAppFaceGateVisitorService.checkAdd(request);
        return ResultUtil.success(data);
    }
}
